package com.ruoyi.web.controller.rjgc;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.littleRedBook.domain.pojo.Product;
import com.ruoyi.littleRedBook.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/product")
@Api(tags = "商品核心管理")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/add")
    @ApiOperation("商品上架（提交审核）")
    public AjaxResult addProduct(@ApiParam("商品信息") @RequestBody Product product) {
        return productService.addProduct(product);
    }

    @PutMapping("/edit")
    @ApiOperation("编辑商品（仅未售出/未下架商品可编辑）")
    public AjaxResult editProduct(@ApiParam("商品信息") @RequestBody Product product) {
        return productService.editProduct(product);
    }

    @PutMapping("/offline/{id}")
    @ApiOperation("商品下架")
    public AjaxResult offlineProduct(@ApiParam("商品ID") @PathVariable Long id) {
        return productService.offlineProduct(id);
    }

    @GetMapping("/my/onsale")
    @ApiOperation("获取当前用户的在售商品（审核通过未售出）")
    public AjaxResult getMyOnsaleProducts() {
        return productService.getMyOnsaleProducts();
    }

    @GetMapping("/my/sold")
    @ApiOperation("获取当前用户的已售商品")
    public AjaxResult getMySoldProducts() {
        return productService.getMySoldProducts();
    }

    @GetMapping("/my/pending")
    @ApiOperation("获取当前用户的待审核商品")
    public AjaxResult getMyPendingProducts() {
        return productService.getMyPendingProducts();
    }

    @GetMapping("/my/rejected")
    @ApiOperation("获取当前用户的审核驳回商品")
    public AjaxResult getMyRejectedProducts() {
        return productService.getMyRejectedProducts();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("获取商品详情（浏览量自动+1）")
    public AjaxResult getProductDetail(@ApiParam("商品ID") @PathVariable Long id) {
        return productService.getProductDetail(id);
    }

    @GetMapping("/list")
    @ApiOperation("商品列表浏览（分页，默认按创建时间倒序）")
    public AjaxResult getProductList(
            @ApiParam("页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页条数，默认10") @RequestParam(defaultValue = "10") Integer pageSize) {
        return productService.getProductList(pageNum, pageSize);
    }

    @GetMapping("/search")
    @ApiOperation("商品搜索（关键词+分类+价格范围）")
    public AjaxResult searchProducts(
            @ApiParam("搜索关键词，可为空") @RequestParam(required = false) String keyword,
            @ApiParam("分类ID，0表示所有分类") @RequestParam(defaultValue = "0") Long categoryId,
            @ApiParam("最低价格，可为空") @RequestParam(required = false) BigDecimal minPrice,
            @ApiParam("最高价格，可为空") @RequestParam(required = false) BigDecimal maxPrice,
            @ApiParam("页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页条数，默认10") @RequestParam(defaultValue = "10") Integer pageSize) {
        return productService.searchProducts(keyword, categoryId, minPrice, maxPrice, pageNum, pageSize);
    }
}
